﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaterCloud.Code;
using WaterCloud.DataBase;
using WaterCloud.Domain.Entity.STZBManage;

namespace WaterCloud.Service.STZBManage
{
    /// <summary>
    /// 武器服务类
    /// </summary>
    public class WeaponsService : BaseService<Weapons>, IDenpendency
    {
        public WeaponsService(ISqlSugarClient context) : base(context)
        {
        }


        /// <summary>
        /// 添加武器数据
        /// </summary>
        /// <param name="weapons">数据信息</param>
        /// <returns></returns>
        public async Task AddWeapons(Weapons weapons)
        {
            weapons.CreateTime = DateTime.Now; //创建时间
            await repository.Insert(weapons);
        }

        /// <summary>
        /// 添加或修改武器数据
        /// </summary>
        /// <param name="weapons">数据信息</param>
        /// <returns></returns>
        public async Task AddOrDetails(Weapons weapons)
        {
            if (weapons.Id <= 0) //添加
            {
                await AddWeapons(weapons);
            }
            else //修改
            {
                weapons.UpdateTime = DateTime.Now;
                await repository.Db_Context().Updateable(weapons).UpdateColumns(x => new {x.Name,x.Entry,x.Gear,x.Price, x.UpdateTime }).ExecuteCommandAsync();
            }
        }

        /// <summary>
        /// 根据id查询武器信息
        /// </summary>
        /// <param name="id">数据id</param>
        /// <returns></returns>
        public async Task<Weapons> GetFormJson(int id)
        {
            var data=await repository.IQueryable().Where(x => x.Id == id).FirstAsync();
            return data;
        }

        /// <summary>
        /// 获取武器列表
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">关键词</param>
        /// <returns></returns>
        public async Task<List<Weapons>> Get_Weapons_GridJson(SoulPage<Weapons> pagination, string keyword)
        {
            var data = repository.IQueryable()
                .WhereIF(!string.IsNullOrEmpty(keyword), x => x.Name.Contains(keyword));
            //权限过滤（保证分页参数存在）
            data = GetDataPrivilege("x", "", data);
            return await data.ToPageListAsync(pagination);
        }

        /// <summary>
        /// 获取武器数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<Weapons>> WeaponsTable()=> await repository.IQueryable().OrderByDescending(x=>x.CreateTime).ToListAsync();

        /// <summary>
        /// 删除武器数据
        /// </summary>
        /// <param name="ids">数据id，多个值用逗号隔开</param>
        /// <returns></returns>
        public async Task Delete_Weapons(string ids)
        {
            string[] idArr = ids.Split(',');
            await repository.Delete(x => idArr.Contains(x.Id.ToString()));
        }
    }
}
